<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016-06-21
 * Time: 17:25
 */

function filter_id_to_ralname($ids){
    return  D('MobileAdmin')->get_names_by_ids($ids);
}

function filter_device_status($status=999){
    //0关闭，1运行，-1故障，-2维修
    $statuss = array(
        1 => LANG('open_device'),
        0 => LANG('off_device'),
//        2 => LANG('running'),
//        -1 => LANG('fixing'),
        -2 => LANG('fault'),
    );
    $return = array();
    foreach($statuss as $k => $v){
        if($status == $k)
            continue;
        $return[] = array('status' => $k, 'comment' => $v);
    }
    return $return;
}

function get_device_status($status=999){
    $statuss = array(
        0 => LANG('off_device'),
        1 => LANG('open_device'),
//        -1 => LANG('fixing'),
        -2 => LANG('fault'),
    );
    return $statuss[$status]?$statuss[$status]:'';
}

function filter_common_status($status=-1){
    $str = '';
    switch($status){
        case 0:
            $str = '停用';
            break;
        case 1:
            $str = '启用';
            break;
    }
    return $str;
}

function filter_deivce_status_icon($status=999){
    $str = '';
    switch($status){
        case 0:
            $str = '/Public/module/manage/image/stop.gif';
            break;
        case 1:
            $str = '/Public/module/manage/image/fun1.gif';
            break;
        case -1:
            $str = '/Public/module/manage/image/warring.gif';
            break;
        case -2:
            $str = '/Public/module/manage/image/repair.jpg';
            break;
        default:
            $str = '/Public/module/manage/image/stop.gif';
            break;
    }
    return $str;
}

function get_pump_type_option($type = 0){
    $types = array(
        1 => LANG('rain_pump'),
        2 => LANG('sewage_pump'),
        -1 => '未分配',
    );
    $str = '';
    foreach($types as $k => $v){
        $str .= '<option value="'.$k.'"';
        if($type == $k){
            $str .= ' selected="selected"';
        }
        $str .= '>'.$v.'</option>';
    }
    return $str;
}

function timestamp_to_time($type, $time){
    $int = 0;
    switch($type){
        case 'hour':
            $int = $time/3600;
            break;
    }
    return $int;
}

function array_to_option($array=array()){
    $str = '<option value="">全部</option>';
    foreach($array as $k => $v){
        $str .= '<option value="'.$k.'"';
        if($array == $k){
            $str .= ' selected="selected"';
        }
        $str .= '>'.$v.'</option>';
    }
    return $str;
}

function array_to_option_no_all_text($array=array()){
    $str = '<option value=""></option>';
    foreach($array as $k => $v){
        $str .= '<option value="'.$k.'"';
        if($array == $k){
            $str .= ' selected="selected"';
        }
        $str .= '>'.$v.'</option>';
    }
    return $str;
}

function pump_station_base_types_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '市区泵站',
        2 => '尾水泵站',
        3 => '环湖截污污水泵站',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_station_types_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '雨水',
        2 => '污水',
        3 => '合流',
        4 => '合建',
        5 => '再生水',
        0 => '待分配'
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_station_types_second_option($type=-1, $is_return_option = true){
    $types = array(
//        0 => '无',
        1 => '隧道泵站',
        2 => '泵闸',
        3=> '枢纽泵站',
        4 => '其他',
        0 => '待分配',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_station_scope_option($type=-1, $is_return_option = true){
    $types = array(
        0 => '无',
        1 => '特大型',
        2 => '大型',
        3=> '中型',
        4 => '小型',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_station_area_option($type=-1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '五华区',
        2 => '盘龙区',
        3=> '西山区',
        4 => '官渡区',
        5 => '呈贡区',
        6 => '高新区',
        7 => '经开区',
        8 => '度假区',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_station_status_option($type=-1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '已建',
        2 => '在建',
        3=> '废弃',
        4=> '规划',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function ps_run_typ($type=0, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '常年运转',
        2 => '季节运转',
        3=> '偶尔运转',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function ps_guard_typ($type=0, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '无人值守',
        2 => '有人值守',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function ps_shift_sys($type=0, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '四班三运转',
        2 => '三班二运转',
        3 => '其他',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function is_right($type=1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '是',
        2 => '否',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function is_have($type=1, $is_return_option = true){
    $types = array(
        0=>'未知',
        1 => '有',
        2 => '无',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function ps_datasrc($type=0, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '设计图',
        2 => '竣工图',
        3 => '现场测绘',
        4 => '人工估计',
        5 => '其他',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_pump_type_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '雨水泵',
        2 => '污水泵',
        0 => '未设置',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_pump_usemodel_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '直接启动',
        2 => '降压启动',
        3 => '变频启动',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function cb_shp($type=-1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '矩形',
        2 => '圆形',
        3 => '多边形',
        4 => '其他',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function cb_rws_separate($type=-1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '分流堰',
        2 => '闸门',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function val_typ($type=-1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '进水阀',
        2 => '出水阀',
        3 => '排气阀',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function val_orientation($type=-1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '顺时针',
        2 => '逆时针',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function gate_typ($type=-1, $is_return_option = true){
    $types = array(
        0 => '未知',
        1 => '进水闸',
        2 => '检修闸',
        3 => '出水闸',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function device_status_option($type=-1, $is_return_option = true){
    $types = array(
        0 => LANG('off_device'),
        1 => LANG('open_device'),
        -2 => LANG('fault'),
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function gate_status_option($type=-1, $is_return_option = true){
    $types = array(
        0 => '关闸',
        1 => '开闸',
        -2 => LANG('fault'),
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function station_status_option($type=-1, $is_return_option = true){
    $types = array(
        0 => '关闭',
        1 => '使用中',
        -2 => LANG('fault'),
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function weather_status_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '天晴',
        2 => '小雨',
        3 => '中雨',
        4 => '大雨',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function pump_machine_type_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '除污机',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function csdt_types_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '雨水',
        2 => '污水',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function csdt_types_second_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '雨水',
        2 => '污水',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function csdt_pump_type_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '雨水泵',
        2 => '污水泵',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function csdt_pump_usemodel_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '直接启动',
        2 => '降压启动',
        3 => '变频启动',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function gate_cont_type_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '手动控制',
        2 => '液压控制',
        3 => '电动控制',
        4 => '其他',
        0 => '未配置',
        -1 => '未配置',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function gate_swit_sty_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '电手动螺杆',
        2 => '手摇螺杆',
        3 => '手摇链条',
        4 => '电动卷扬',
        5 => '电动螺杆',
        6 => '其他',
        0 => '未配置',
        -1 => '未配置',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function ele_type($type=-1, $is_return_option = true){
    $types = array(
        1 => '动力表',
        2 => '照明表',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function gate_facilities_type_option($type=-1, $is_return_option = true){
    $types = array(
        1 => '泵站',
        2 => '调蓄池',
        3 => '河道闸门',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function sign_option($type=-1, $is_return_option = true){
    $types = array(
        -1 => '迟到',
        -2 => '早退',
        1 => '上班',
        2 => '下班',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function is_quick_report($type=-1, $is_return_option = true){
    $types = array(
        0 => '未及时填报',
        1 => '及时填报',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function log_run_status($type=-1, $is_return_option = true){
    $types = array(
        0 => '运行',
        1 => '运行',
        -2 => LANG('fault'),
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}

function fix_status($type=-1, $is_return_option = true){
    $types = array(
        1 => '已报修，未派人',
        2 => '维修中',
        3 => '维修完成',
        4 => '作废',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    $str = '<option value="">全部</option>';
    foreach($types as $k => $v){
        $str .= '<option value="'.$k.'"';
        if($types == $k){
            $str .= ' selected="selected"';
        }
        $str .= '>'.$v.'</option>';
    }
    return $str;
}

function valdate_time($time){
    if(!$time||$time=='0000-00-00 00:00:00')
        return false;
    else
        return true;
}

function clear_array_to_new_array($array){
    $return_array = array();
    foreach($array as $v){
        if($v != '')
            $return_array[] = $v;
    }
    return $return_array;
}

function get_field_array($array, $first_key, $field=false){
    //按照表来查找
    if(!$field){
        $field_array = $array[$first_key]['children'];
        return $field_array;
    }
    //根据条件来查找字段信息
    $return_field_array = array();
    foreach($array as $table_name => $field_array){
        if($table_name != $first_key)
            continue;
        foreach($field_array['children'] as $fields){
            if($fields['field'] == $field){
                $return_field_array = $fields;
                break;
            }
        }
    }
    return $return_field_array;
}

function filter_field_array($field_array, $map){
    foreach($field_array as $field_array){
        foreach($field_array['children'] as $fields){
            foreach($map as $field => $condition){
                if($field == $fields['field']){
                    if($fields['type'] == 2 || $fields['type'] == 0){
                        $map[$field][0] = 'eq';
                        $map[$field][1] = str_replace('%', '', $map[$field][1]);
                    }else if($fields['type'] == 3 || $fields['type'] == 4){
                        $map[$field][0] = trim(str_replace('%', '', $map['sysbom'][1]));
                        $map[$field][1] = intval(str_replace('%', '', $map[$field][1]));
                        unset($map['sysbom']);
                    }
                }
            }
        }
    }
    return $map;
}

function get_condition_array(){
    $array = array(
        'EQ' => '等于',
        'EGT' => '大于',
        'ELT' => '小于',
    );
    $str = '<option></option>';
    foreach($array as $k => $v){
        $str .= '<option value="'.$k.'"';
        $str .= '>'.$v.'</option>';
    }
    return $str;
}

function stations_add_all_text($list, $text='name'){
    $all_station = array(array('id'=>'',$text=>''),array('id'=>'',$text=>'全部'));
    $list = array_merge($all_station, $list);
    return $list;
}

function my_array_filter_to_text($array, $split_s = ','){
    if(!is_array($array)){
        $array = explode($split_s, $array);
    }
    $array = array_filter($array);
    $array = implode($split_s, $array);
    return $array;
}

function get_hour_array($type=-1, $is_return_option = true){
    $types = array(
    );
    for($i=0;$i<24;$i++){
        $types[$i] = substr('00' . $i, -2).'时';
    }
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    $str = '<option value=""></option>';
    foreach($types as $k => $v){
        $str .= '<option value="'.$k.'"';
        if($type == $k){
            $str .= ' selected="selected"';
        }
        $str .= '>'.$v.'</option>';
    }
    return $str;
}

function records_check_status($records){
    $checked_num = 0;
    $nochecked_num = 0;
    $check_date = '';
    foreach($records as $record){
        if($record['ischecked'] == 0){
            $nochecked_num++;
        }else  if($record['ischecked'] == 1){
            $checked_num++;
            if($nochecked_num > 0)
                $check_date = $record['date'];
        }
    }
    if($checked_num == count($records)){
        return '已审核';
    }else if($nochecked_num == count($records)){
        return '未审核';
    }else if($checked_num > 0 && $nochecked_num > 0){
        return '已审核至'.$check_date;
    }
}

function is($type=-1, $is_return_option = true){
    $types = array(
        0 => '否',
        1 => '是',
    );
    if(!$is_return_option){
        return $types[$type]?$types[$type]:-1;
    }
    return array_to_option($types);
}